// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_pickup = require("./accountBulkstockListing.css");
	var databind = require("databind");
	var financeId;
	var inventoryId = [];
	var remark;
	var tools = require("tools");
	var caseSizeMap = new tools.Map();
	var storage = window.sessionStorage;
	var productType;

	var init = function(page) {
		productType = page.params.productType;
		financeId = storage.getItem("financeId");
		if(financeId==null){
			financeId = page.params.financeId;
		}else{
			storage.removeItem("financeId");
		}
		
		caseSizeMap.toString = function() {
			var str = "";
			for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
				if (this.container[keys[i]] == 0) {
					continue;
				}
				str = str  + this.container[keys[i]] + "X" + keys[i] + "<br/>";
			}
			return (str == "" ? "箱体情况：无" : "箱体情况：" + str);
		}
		
		inquireFinanceContainerInventoryPageList();
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}
		//点击项目基本详情financeDetail
		$(".financeDetail").unbind("click").click(function(){
			router.route("account/accountDetail.html?financeId="+financeId,true);
                	
		});
		
		$("#accountBulkstockListing .bulkstocklistingOperate").hover(function(){
			$(this).find(".detainedDetail").css({"z-index": "202"});
		});
		
		$("#detainedApplicationSubmitBtn").unbind("click").click(function(){
			inventoryId = [];
			remark = "";
			
			var inventorys = $("#accountDetainedApplication .detainedApplicationChecked:checked");
			$.each(inventorys, function() {
				inventoryId.push($(this).parents("tr").attr("inventoryId"));
			});
			
			remark = $.trim($("#detainedApplicationReason").val());
			
			if(inventoryId.length == 0){
				$("#accountDetainedApplication .error_info").html("请选择要扣货的货箱！");
				$("#accountDetainedApplication .error_info").removeClass("none");
			}else if(remark==""){
				$("#accountDetainedApplication .error_info").html("请填写扣货缘由！");
				$("#accountDetainedApplication .error_info").removeClass("none");
			}else{
				$("#accountDetainedApplication .error_info").addClass("none");
				tools.protectDoubleClick();
				createDetainedApplication();
			}
		});
	};

	//查询理货任务列表
	var inquireFinanceContainerInventoryPageList = function() {
		base.fnAjax({
			action: "inquireFinanceContainerInventoryPageList.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#bulkstockListingTpl").html();
					if (productType == 5 || productType == 6) {
						$(".case_info").addClass("none");
						tpl = $("#bulkstockListingTpl1").html();
					}
					var html = juicer(tpl, data);
					$("#bulkstockListingBody").html(html);
					
					$(".detainedDetail").unbind("click").click(function(){
						router.route("account/accountTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId")+"&financeId="+financeId);
					});
					
					$(".logisticsDetailInfo").unbind("click").click(function(){
						router.route("account/inquireLogisticsInfoDetail1.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
					
					//放货状态查询
					$(".selectStatus").unbind("click").click(function(){
						selectStatus($(this), $(this).parents("tr").attr("waybillNumber"), $(this).parents("tr").attr("caseNumber"));
					});
					
					caseSizeMap.put("10GP", 0);
					caseSizeMap.put("20GP", 0);
					caseSizeMap.put("40GP", 0);
					caseSizeMap.put("48GP", 0);
					caseSizeMap.put("10HQ", 0);
					caseSizeMap.put("20HQ", 0);
					caseSizeMap.put("40HQ", 0);
					caseSizeMap.put("48HQ", 0);
					caseSizeMap.put("10RH", 0);
					caseSizeMap.put("20RH", 0);
					caseSizeMap.put("40RH", 0);
					caseSizeMap.put("48RH", 0);
					
					$.each($("#accountBulkstockListing .caseSize"), function(i, val) {
						statisticalCaseSize(caseSizeMap, $.trim($(this).parents("tr").find(".caseSize").html()));
						var e = $(this).parent().parent().find(".financeValuations").html()*1 + $(this).parent().parent().find(".adjustment").html()*1;
						if ($.trim(e) == "") {
							e = 0;
						}
					});
					
					$(".case_info").html(caseSizeMap.toString());
				} else {
				}
			}
		});
	};
	
	var statisticalCaseSize = function(m, c) {
		switch (c){
			case "10GP":
				m.put("10GP", m.get("10GP")*1 + 1);
				break;
			case "20GP":
				m.put("20GP", m.get("20GP")*1 + 1);
				break;
			case "40GP":
				m.put("40GP", m.get("40GP")*1 + 1);
				break;
			case "48GP":
				m.put("48GP", m.get("48GP")*1 + 1);
				break;
			case "10HQ":
				m.put("10HQ", m.get("10HQ")*1 + 1);
				break;
			case "20HQ":
				m.put("20HQ", m.get("20HQ")*1 + 1);
				break;
			case "40HQ":
				m.put("40HQ", m.get("40HQ")*1 + 1);
				break;
			case "48HQ":
				m.put("48HQ", m.get("48HQ")*1 + 1);
				break;
			case "10RH":
				m.put("10RH", m.get("10RH")*1 + 1);
				break;
			case "20RH":
				m.put("20RH", m.get("20RH")*1 + 1);
				break;
			case "40RH":
				m.put("40RH", m.get("40RH")*1 + 1);
				break;
			case "48RH":
				m.put("48RH", m.get("48RH")*1 + 1);
				break;
			default:
				break;
		}
	}
	
	//放货状态查询
	var selectStatus = function(dom, waybillNumber, caseNumber) {
		base.fnAjax({
			action: "selectStatus.json",
			async: false,
			oData: {
				waybillNumber: waybillNumber,
				caseNumber: caseNumber
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas[0].returnStatus) {
						dom.parent().find("span").html("已放货");
					} else {
						dom.parent().find("span").html("未放货");
					}
				} else {
					base.alert(3, "查询失败");
					dom.parent().find("span").html("未知");
				}
			}
		});
	};
	
	//扣货申请
	var createDetainedApplication = function() {
		base.fnAjax({
			action: "createDetainedApplication.json",
			async: false,
			oData: {
				financeId: 3,
				remark: remark,
				inventoryId: inventoryId
			}, //传递参数
			fnSuccess: function(data) {
				tools.cancelProtectDoubleClick();
				if (data.status == 200) {
					router.route("account/accountDetail.html?financeId="+financeId);					
				} else {
				}
			}
		});
	};

	return {
		init: init
	};
});